package com.letao.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;

/**
 * Created by ac on 2017/1/10.
 */
public class FtpUtil {


    /**
     * 上传文件到Ftp服务器上
     * @param remote 服务器地址
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param basePath 服务器基本路径
     * @param filePath 文件基本路径
     * @param fileName 文件名称
     * @param inputStream 文件输入流
     * @return 是否成功
     * @throws IOException
     */
    public static boolean uploadFile(String remote, int port, String username, String password, String basePath, String filePath, String fileName,
                                  InputStream inputStream) throws IOException {
        boolean result = false;
        //新建一个ftpclient
        FTPClient ftpClient = new FTPClient();
//        添加服务器地址和端口
        ftpClient.connect(remote, port);
//        添加用户名和密码
//        登录ftp服务器
        ftpClient.login(username, password);

        //查看链接状态，若没连上返回false
        int replyCode = ftpClient.getReplyCode();
        if(!FTPReply.isPositiveCompletion(replyCode)){
            ftpClient.disconnect();
            return result;
        }

        //判断文件路径存不存在，若不存在创建目录
        if(!ftpClient.changeWorkingDirectory(basePath + filePath)){
            String[] split = filePath.split("/");
            String tempPath = basePath;
            for (String st : split) {
                if(StringUtils.isNotBlank(st)){
                    tempPath = tempPath + "/" + st;
                    if(!ftpClient.changeWorkingDirectory(tempPath)){
                        if(!ftpClient.makeDirectory(tempPath)){
                            return result;
                        } else {
                            ftpClient.changeWorkingDirectory(tempPath);
                        }
                    }
                }
            }
        }

        //  改变操作文件夹路径
        ftpClient.changeWorkingDirectory(basePath + filePath);
        //        设置传输格式，默认是文本格式
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        result = ftpClient.storeFile(fileName, inputStream);
        inputStream.close();
        ftpClient.logout();
        ftpClient.disconnect();
        return result;
    }

    /**
     * 利用ftp从服务器上下载文件
     * @param remote 服务器地址
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param fileBasePath 服务器文件基本路径
     * @param fileName 文件名称
     * @param localPath 下载到本地的路径
     * @return
     */
    public static boolean downloadFile(String remote, int port, String username, String password, String fileBasePath, String fileName,
                                       String localPath) throws IOException {
        boolean result = false;
        //新建一个ftpclient
        FTPClient ftpClient = new FTPClient();
//        添加服务器地址和端口
        ftpClient.connect(remote, port);
//        添加用户名和密码
//        登录ftp服务器
        ftpClient.login(username, password);

        //查看链接状态，若没连上返回false
        int replyCode = ftpClient.getReplyCode();
        if(!FTPReply.isPositiveCompletion(replyCode)){
            ftpClient.disconnect();
            return result;
        }
        //改变ftp工作目录
        if(ftpClient.changeWorkingDirectory(fileBasePath)){
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file :
                    files) {
                if (file.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + fileName);
                    FileOutputStream outputStream = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(fileName, outputStream);
                    outputStream.close();
                }
            }

            ftpClient.logout();
            ftpClient.disconnect();;
            result = true;
        }
        return result;
    }

    public static void main(String[] args){
        try {
            FileInputStream fileInputStream = new FileInputStream("c:\\50x.html");
            FtpUtil.uploadFile("192.168.0.238", 21, "ftpuser", "123456", "/home/ftpuser/www", "/2017/1/10", "50x.html", fileInputStream);
//            FtpUtil.downloadFile("192.168.0.238", 21, "ftpuser", "123456", "/home/ftpuser/www", "50x.html", "C:\\");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
